/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.bezrabotnih.cabinet.client.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.bezrabotnih.client.managed.request.JobTypeProxy;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.place.shared.Place;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.RadioButton;

/**
 * Sample implementation of {@link CreateEditJobView}.
 */
public class CreateEditJobViewImpl extends Composite implements CreateEditJobView {

	interface Binder extends UiBinder<Widget, CreateEditJobViewImpl> {
	}
	
	private static final Binder binder = GWT.create(Binder.class);

	private static CreateEditJobView instace; 
	
	private Presenter listener;
	@UiField
	Button saveButton;
	@UiField TextBox name;
	@UiField DateBox startDate;
	@UiField DateBox deadlineDate;
	@UiField ListBox jobType;
	@UiField TextArea description;
	@UiField IntegerBox price;
	@UiField RadioButton isHourly;
	@UiField RadioButton forWork;
	
	List<String> jobTypeIds=new ArrayList<String>();
	
	boolean isAllJobTypesListInited;

	public CreateEditJobViewImpl() {
		initWidget(binder.createAndBindUi(this));
		init();
		
	}

	public static CreateEditJobView instance(){
		if (instace==null){
			instace=new CreateEditJobViewImpl();
		}
		return instace;	


	}

	private void init(){
		startDate.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT)));
		startDate.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT)));
	}

	

	@Override
	public void setPresenter(Presenter listener) {
		this.listener = listener;
	}

	@UiHandler("saveButton")
	void onSaveButtonClick(ClickEvent event) {
		this.listener.saveClicked();
	}

	@Override
	public String getName() {
		return name.getText();	
		
	}

	@Override
	public String getJobTypeId() {
		if (jobTypeIds!=null&&jobTypeIds.size()>0) {
			return jobTypeIds.get(jobType.getSelectedIndex());
		} 
		return null;
	}

	@Override
	public boolean isHourly() {
		return isHourly.getValue();
	}

	@Override
	public String getDescription() {
		return description.getText();
	}

	@Override
	public Integer getPrice() {
		return price.getValue();
	}

	@Override
	public Date getDeadlineDate() {
		return deadlineDate.getValue();
	}

	@Override
	public Date getStartDate() {
		return startDate.getValue();
	}

	@Override
	public void initAllJobTypesList(List<JobTypeProxy> jobTypeProxies) {
		for (JobTypeProxy jobTypeProxy:jobTypeProxies){
			jobType.addItem(jobTypeProxy.getName());
			jobTypeIds.add(jobTypeProxy.getId());
		}
		isAllJobTypesListInited=true;
	}

	

	@Override
	public boolean resetForms() {
		name.setText("");
		if (jobType.getItemCount()>0) jobType.setSelectedIndex(0);
		isHourly.setValue(false);
		description.setText("");
		price.setText("");
		deadlineDate.setValue(null);
		startDate.setValue(null);
		return false;
	}

	@Override
	public boolean isAllJobTypesListInited() {
		return isAllJobTypesListInited;
	}

	@Override
	public void setName(String name) {
		this.name.setText(name);
		
	}

	@Override
	public void setJobType(String jobTypeId) {
		if (jobTypeIds!=null&&jobTypeIds.size()>0){
			this.jobType.setSelectedIndex(jobTypeIds.indexOf(jobTypeId));		
		}
	}

	@Override
	public void setHourly(boolean isHously) {
		this.isHourly.setValue(isHously);
		this.forWork.setValue(!isHously);
		
	}

	@Override
	public void setDescription(String description) {
		this.description.setText(description);
		
	}

	@Override
	public void setPrice(Integer price) {
		this.price.setValue(price);
		
	}

	@Override
	public void setDeadlineDate(Date deadline) {
	    this.deadlineDate.setValue(deadline);
	}

	@Override
	public void setStartDate(Date startDate) {
		this.startDate.setValue(startDate);
		
	}

	@Override
	public void setEditView(boolean isEditView) {
		this.deadlineDate.setEnabled(!isEditView);
		this.startDate.setEnabled(!isEditView);
		this.isHourly.setEnabled(!isEditView);
		this.forWork.setEnabled(!isEditView);
		this.price.setEnabled(!isEditView);
		this.jobType.setEnabled(!isEditView);
		this.name.setEnabled(!isEditView);
		
	}
}
